package com.second.app.treenode;

public class TreeNodeDemo {


    public static void main(String[] args) {

        TreeNode root = new TreeNode(1);

        root.left  = new TreeNode(2);

        root.right  = new TreeNode(3);

        root.left.left=  new TreeNode(4);

        root.left.right=  new TreeNode(5);

        System.out.print("前序遍历: \n");
        preOrder(root);
        System.out.println();

        System.out.print("中序遍历: \n");
        inOrder(root);
        System.out.println();

        System.out.print("后序遍历: \n");
        postOrder(root);
        System.out.println();

    }

    //根 左 右

    public static void preOrder(TreeNode node){
       if(node !=null){
           System.out.println(node.val+" ");
           preOrder(node.left);
           preOrder(node.right);
       }
    }


    // 左 根 右
    public static void inOrder(TreeNode node){
        if(node !=null){
            inOrder(node.left);
            System.out.println(node.val+" ");
            inOrder(node.right);
        }
    }


    // 左 右 根
    public static void postOrder(TreeNode node){
        if(node !=null){
            postOrder(node.left);
            postOrder(node.right);
            System.out.println(node.val+" ");
        }
    }



}

